/********************************************************************************
*                 Copyright (C) 2011 - Las Venturas Playground                  *
*                      http://sa-mp.nl | http://lvp-iv.com                      *
*                                                                               *
*    This program is free software: you can redistribute it and/or modify       *
*    it under the terms of the GNU General Public License as published by       *
*    the Free Software Foundation, either version 3 of the License, or          *
*    (at your option) any later version.                                        *
*                                                                               *
*    This program is distributed in the hope that it will be useful,            *
*    but WITHOUT ANY WARRANTY; without even the implied warranty of             *
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
*    GNU General Public License for more details.                               *
*                                                                               *
*    You should have received a copy of the GNU General Public License          *
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.      *
*                                                                               *
*********************************************************************************
*                                                                               *
*  Las Venturas Playground - Core/Vehicle/commands.nut                          *
*                                                                               *
*  This file holds all vehicle related commands.                                *
*  @author: Matthias <matthias@sa-mp.nl>                                        *
*                                                                               *
********************************************************************************/

local cmdHandler = CommandHandler();

/**
 * This is the /respawn command, the command respawns
 * all vehicles currently loaded in the server.
 * @author: Sander <sander@sa-mp.nl>
 *
 * @param playerid The playerid that used the command.
 * @param commandParams The parameters passed on to the command.
 * @param commandText The entire command as a string.
 */
local function cmdRespawn (playerid, cmdWords, cmdStr)
{     
    // Create a new instance of the Vehicle class
    local Vehicle = Vehicle ();
    
    // Delete all vehicles.
    Vehicle.deleteVehicles ();
    
    // And now load the vehicles from the database.
    Vehicle.loadVehicles ();
}

cmdHandler.registerCommand ("/respawn", Command (cmdRespawn), COMMAND_LEVEL_ADMINISTRATOR);

/**
 * This is the /nitro command, the command gives
 * the car the player is in a speed boost.
 * @author: Matthias <matthias@sa-mp.nl>
 *
 * @param playerid The playerid that used the command.
 * @param commandParams The parameters passed on to the command.
 * @param commandText The entire command as a string.
 */
local function cmdNitro (playerid, cmdWords, cmdStr)
{
    // Make sure he's in a car
    if(isPlayerInAnyVehicle(playerid))
    {
        // Get the vehicle velocity
        local aVelocity = getVehicleVelocity(getPlayerVehicleId(playerid));
        
        // Multiply the x, y, and z axis times 15.
        aVelocity[0] *= 15;
        aVelocity[1] *= 15;
        aVelocity[2] *= 15;
        
        // And set it.
        setVehicleVelocity(getPlayerVehicleId(playerid), aVelocity[0], aVelocity[1], aVelocity[2]);
        
        // Inform the rest of the players.
        sendMessageToAll ("* " + getPlayerName (playerid) + " is now using nitro", COLOR_GREEN);
    }
    else
    {
        // Not in a car, inform the user.
        sendPlayerErrorMessage (playerid, "You need to be in a car to use this command.");
    }
}

cmdHandler.registerCommand ("/nitro", Command (cmdNitro), COMMAND_LEVEL_PLAYER);

/**
 * The /vr command, allowing administrators to repair their vehicle.
 * @author: Sander <sander@sa-mp.nl>
 *
 * @param playerid The playerid that used the command.
 * @param commandParams The parameters passed on to the command.
 * @param commandText The entire command as a string.
 */
local function cmdVr (playerid, cmdWords, cmdStr)
{
    if (isPlayerOnFoot (playerid)) 
    {
        sendPlayerErrorMessage( playerid, "Forgot your vehicle?" );
        return;
    }
    
    setVehicleEngineHealth (getPlayerVehicleId (playerid), 1000);
    sendPlayerMessage( playerid, "* Your vehicle has been repaired.", COLOR_GREEN );
}

cmdHandler.registerCommand ("/vr", Command (cmdVr), COMMAND_LEVEL_MODERATOR);


/**
 * The /savevehicle command, this command saves a vehicle in the database.
 * @author: Sander <sander@sa-mp.nl>
 *
 * @param playerid The playerid that used the command.
 * @param commandParams The vehicle name or ID to spawn.
 * @param commandText The entire command as a string.
 */
local function cmdSaveVehicle (playerid, cmdWords, cmdStr)
{
    // Is the user in a vehicle?
    if (isPlayerInAnyVehicle (playerid))
    {
        // Did he enter any parameters?
        if (cmdWords.len() < 3)
        {
            sendPlayerUsageMessage (playerid, "/savevehicle [class] [random (1 = random)]");
            sendPlayerMessage (playerid, "* Class info:", COLOR_WHITE);
            sendPlayerMessage (playerid, "0 = cheap vehicles", COLOR_WHITE);
            sendPlayerMessage (playerid, "1 = normal vehicles", COLOR_WHITE);
            sendPlayerMessage (playerid, "2 = sports cars", COLOR_WHITE);
            sendPlayerMessage (playerid, "3 = helicopters", COLOR_WHITE);
            sendPlayerMessage (playerid, "4 = boats", COLOR_WHITE);
            sendPlayerMessage (playerid, "5 = bikes", COLOR_WHITE);
            sendPlayerMessage (playerid, "6 = industrial", COLOR_WHITE);
            sendPlayerMessage (playerid, "7 = emergency", COLOR_WHITE);
            return;
        }

        // Make sure what he typed is a number.
        if (isNumeric(cmdWords[1]) == false)
        {
            sendPlayerErrorMessage (playerid, "The class has to be a number");
            return;
        }
        
        if (isNumeric(cmdWords[2]) == false)
        {
            sendPlayerErrorMessage (playerid, "The random parameter has to be a number");
            return;
        }
        
        // It's numeric, get the integer.
        local vehicleClass = cmdWords[1].tointeger ();
        local isRandom = cmdWords[2].tointeger ();
        
        // Make sure the class and the random parameters are correct.
        if (isRandom < 0 || isRandom > 1)
        {
            sendPlayerErrorMessage (playerid, "The random parameter has to be either 0 (static) or 1 (random)");
            return;
        }
        
        if (vehicleClass < 0 || vehicleClass > 7)
        {
            sendPlayerErrorMessage (playerid, "The class has to be between 0 and 7");
            return;
        }
        
         // Call the saveVehicle method.
        local Vehicle = Vehicle ();
        Vehicle.saveVehicle (getPlayerVehicleId (playerid), vehicleClass, isRandom);
         
        sendPlayerMessage (playerid, "Vehicle ID #" + getPlayerVehicleId (playerid) + " has been saved in the database.");
    }
    else
    {
        sendPlayerErrorMessage (playerid, "You have to be in a car to use this command!");
    }
}

cmdHandler.registerCommand ("/savevehicle", Command (cmdSaveVehicle), COMMAND_LEVEL_MANAGEMENT);

/**
 * The /v or /vehicle command, allowing crew to spawn a vehicle.
 * @author: Sander <sander@sa-mp.nl>
 *
 * @param playerid The playerid that used the command.
 * @param commandParams The vehicle name or ID to spawn.
 * @param commandText The entire command as a string.
 */
local function cmdVehicle (playerid, cmdWords, cmdStr)
{
    if (cmdWords.len () < 2) 
    {
        sendPlayerUsageMessage (playerid, "/v [VehicleID or vehiclename]");
        return;
    }
    
     // Create a new instance of the Vehicle class
    local Vehicle = Vehicle ();
    
    // Get the ID from the model name.
    local matchNameWithModel = Vehicle.getIDFromName (cmdWords[1]);

    if (matchNameWithModel == -1)
    {
        sendPlayerErrorMessage (playerid, "No matching vehiclenames found");
        return;
    }

    local model = matchNameWithModel.tointeger ();
    
    // Is the vehicle valid?
    if (Vehicle.isValidID (model) == false)
    {
        sendPlayerErrorMessage (playerid, "Invalid model id!" );
        return;
    }
    
    local pos;
    if (isPlayerInAnyVehicle (playerid))
    {
        pos = getVehicleCoordinates (getPlayerVehicleId (playerid));
    }
    else
    {
        pos = getPlayerCoordinates (playerid);
    }
    
    local rotation_z = getPlayerHeading (playerid);
    local rand_color1 = random (0, 133);
    local rand_color2 = random (0, 133);
    
    if (playerVehicle[playerid] != -1)
    {
        deleteVehicle (playerVehicle[playerid]);
    }
    
    playerVehicle[playerid] = createVehicle (model, pos[0], pos[1], pos[2], 0, 0, rotation_z, rand_color1, rand_color2, 1, 1);
    
    if (playerVehicle[playerid] == INVALID_VEHICLE_ID) 
    {
        sendPlayerErrorMessage (playerid, "* Vehicle limit reached! Cannot create any more.");
        return;
    }
    
    warpPlayerIntoVehicle (playerid, playerVehicle[playerid]);
    sendPlayerMessage (playerid, "* " + getVehicleName(model) + " spawned at your position", COLOR_GREEN);
}

cmdHandler.registerCommand ("/v", Command (cmdVehicle), COMMAND_LEVEL_ADMINISTRATOR);
cmdHandler.registerCommand ("/vehicle", Command (cmdVehicle), COMMAND_LEVEL_ADMINISTRATOR);